/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.DBClass;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.PegawaiBean;

/**
 *
 * @author elfatah
 */
public class LoginServlet extends HttpServlet {

    String username, uname, idPeg, idBag;
    String pass;
    String action;

    PegawaiBean bean = new PegawaiBean();
    DBClass dbobject = new DBClass();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // HttpSession session = request.getSession(); 
        try {
            //        processRequest(request, response);
            //get the values from jsp page
            username = request.getParameter("username");
            pass = request.getParameter("password");
            // action = request.getParameter("action");
            HttpSession session = request.getSession();
            List list = dbobject.cekLogin(username, pass);
            if (list != null) {
                session.setAttribute("pass", pass);
                System.out.println("hehe");
                for (int i = 0; i < list.size(); i++) {
                    bean = (PegawaiBean) list.get(i);
                    uname = bean.getNama();
                    System.out.println(uname);
                    idPeg = bean.getPegawaiId();
                    System.out.println(idPeg);
                    idBag = bean.getIdBagian();
                    System.out.println(idBag);

                }

                //  if (action.equals("LOGIN")) {
                if (idBag.equals("1")) {
                    //  session.setAttribute("isLogin", true);
                    session.setAttribute("user", "sdm");
                    session.setAttribute("id", idPeg);
                    session.setAttribute("username", uname);
                    //  if (!session.getAttribute("user").equals("admin")) {

//                    RequestDispatcher rd = request.getRequestDispatcher("dashboard");
//                    rd.forward(request, response);
                } else if (idBag.equals("2")) {

                    session.setAttribute("user", "produksi");
                    session.setAttribute("id", idPeg);

                    session.setAttribute("username", uname);
                   
                    System.out.println("heh");
//                    RequestDispatcher rd = request.getRequestDispatcher("dashboard");
//                    rd.forward(request, response);
                   
                } else if (idBag.equals("3")) {

                    session.setAttribute("user", "penjualan");
                    session.setAttribute("id", idPeg);

                    session.setAttribute("username", username);  //  if (!session.getAttribute("user").equals("admin")) {

//                    RequestDispatcher rd = request.getRequestDispatcher("dashboard");
//                    rd.forward(request, response);
                } else if (idBag.equals("4")) {

                    session.setAttribute("user", "keuangan");
                    session.setAttribute("id", idPeg);

                    session.setAttribute("username", username); //  if (!session.getAttribute("user").equals("admin")) {

//                    RequestDispatcher rd = request.getRequestDispatcher("dashboard");
//                    rd.forward(request, response);
                } else if (idBag.equals("5")) {

                    session.setAttribute("user", "administrator");
                    session.setAttribute("id", idPeg);

                    session.setAttribute("username", username);   //  if (!session.getAttribute("user").equals("admin")) {

//                    RequestDispatcher rd = request.getRequestDispatcher("dashboard");
//                    rd.forward(request, response);
                } else {
                    RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                    rd.forward(request, response);

                }
//                RequestDispatcher rd = request.getRequestDispatcher("dashboard");
//                rd.forward(request, response);
                 response.sendRedirect("dashboard");
            } else {
                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                rd.forward(request, response);
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
